package com.tang.model.prototype;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/8/15 16:39
 * @Desc 原型模式，使用Cloneable进行拷贝复制，若无实现Clonable接口，回抛出异常：java.lang.CloneNotSupportedException
 * @Remarks
 */
public class PrototypeTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Qitiandasheng qitiandasheng = new Qitiandasheng();
        qitiandasheng.setHeight(100);
        qitiandasheng.setHeight(40);
        qitiandasheng.setSex('m');
        Jingubang jingubang = new Jingubang();
        jingubang.setName("如意金箍棒");
        jingubang.setSize(10000);
        qitiandasheng.setJingubang(jingubang);
        System.out.println(qitiandasheng);
        System.out.println("齐天大圣hashcode：" + qitiandasheng.hashCode());
        System.out.println("齐天大圣金箍棒hashcode：" + qitiandasheng.getJingubang().hashCode());
        System.out.println("=========================");
        Qitiandasheng clone = qitiandasheng.clone();
        System.out.println(clone);
        System.out.println("齐天大圣分身hashcode：" + clone.hashCode());
        System.out.println("齐天大圣分身的金箍棒hashcode：" + clone.getJingubang().hashCode());

        System.out.println("*************************");
        System.out.println("分身去将金箍棒变小,变小。。。");
        clone.getJingubang().setSize(1000);
        System.out.println("齐天大圣hashcode：" + qitiandasheng.hashCode());
        System.out.println("齐天大圣金箍棒hashcode：" + qitiandasheng.getJingubang().hashCode());
        System.out.println("齐天大圣金箍棒Size：" + qitiandasheng.getJingubang().getSize());

        System.out.println("=========================");
        System.out.println(clone);
        System.out.println("齐天大圣分身hashcode：" + clone.hashCode());
        System.out.println("齐天大圣分身的金箍棒hashcode：" + clone.getJingubang().hashCode());
        System.out.println("齐天大圣分身的金箍棒Size：" + clone.getJingubang().getSize());

        System.out.println(qitiandasheng.getJingubang() == clone.getJingubang());
    }
}
